package io.terminus.spzx.user.controller;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.terminus.spzx.consts.UserInfoConstant;
import io.terminus.spzx.model.dto.h5.UserLoginDto;
import io.terminus.spzx.model.dto.h5.UserRegisterDto;
import io.terminus.spzx.model.vo.common.Result;
import io.terminus.spzx.model.vo.common.ResultCodeEnum;
import io.terminus.spzx.model.vo.h5.UserInfoVo;
import io.terminus.spzx.user.service.UserInfoService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

/**
 * @Author lichunlong lcl253137@alibaba-inc.com
 * @Desc 会员用户Controller
 * @Date 2024-11-17
 */
@RestController
@RequiredArgsConstructor
@Tag(name = "会员用户接口")
@RequestMapping(value = "/api/user/userInfo")
public class UserInfoController {

    private final UserInfoService userInfoService;

    @PostMapping("register")
    @Operation(summary = "会员用户注册接口")
    public Result<Void> register(@RequestBody UserRegisterDto userRegisterDto) {
        userInfoService.register(userRegisterDto);

        return Result.build(null, ResultCodeEnum.SUCCESS);
    }

    @PostMapping("login")
    @Operation(summary = "会员用户登录接口")
    public Result<String> login(@RequestBody UserLoginDto userLoginDto) {
        String token = userInfoService.login(userLoginDto);

        return Result.build(token, ResultCodeEnum.SUCCESS);
    }

    @Operation(summary = "获取当前登录用户信息")
    @GetMapping("auth/getCurrentUserInfo")
    public Result<UserInfoVo> getCurrentUserInfo(HttpServletRequest request) {
        String token = request.getHeader(UserInfoConstant.USERINFO_TOKEN_REQUEST_HEADER);
        UserInfoVo userInfoVo = userInfoService.getCurrentUserInfo(token);

        return Result.build(userInfoVo, ResultCodeEnum.SUCCESS);
    }
}
